package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"gitee.com/dreamwood/ez"
	"io/ioutil"
	"os"
	"strings"
)

var target string

func main() {

	flag.StringVar(&target, "t", "src/pages", "目标目录")

	flag.Parse()

	GenerateRoute(target)
}

type VueRoute struct {
	Name          string           `json:"name" bson:"name"`
	Path          string           `json:"path" bson:"path"`
	ComponentPath string           `json:"componentPath" bson:"componentPath"`
	Children      []*VueRouteChild `json:"children" bson:"children"`
}

type VueRouteChild struct {
	Name          string           `json:"name" bson:"name"`
	Path          string           `json:"path" bson:"path"`
	ComponentPath string           `json:"componentPath" bson:"componentPath"`
	Children      []*VueRouteChild `json:"children,omitempty" bson:"children"`
}

func GenerateRoute(root string) {
	//root := "E:/BeeRun/ez_admin_micro/src/pages"
	config := new(VueRoute)
	config.Name = "root"
	config.Path = "/"
	config.ComponentPath = fmt.Sprintf("./layout.vue")
	config.Children = make([]*VueRouteChild, 0)
	files, e := ioutil.ReadDir(root)
	if e != nil {
		println("E1", e.Error())
	}
	for _, file := range files {
		if file.IsDir() {
			conf := new(VueRouteChild)
			conf.Name = ez.CreateRandString(10)
			conf.Path = file.Name()
			path := file.Name()
			conf.ComponentPath = fmt.Sprintf("./%s/layout.vue", path)
			createConfig(root, path, conf)
			config.Children = append(config.Children, conf)
		} else {
			tmp := strings.Split(file.Name(), ".")
			if len(tmp) == 2 && tmp[1] == "vue" && strings.ToLower(tmp[0]) != "layout" {
				conf := new(VueRouteChild)
				conf.Name = ez.CreateRandString(10)
				if strings.ToLower(tmp[0]) != "index" {
					conf.Path = tmp[0]
				}
				conf.ComponentPath = fmt.Sprintf("./%s", file.Name())
				config.Children = append(config.Children, conf)
			}

		}
	}
	e = os.Remove(root + "/routes.js")
	if e != nil {
		println("Del", e.Error())
	}
	content, e := json.MarshalIndent([]*VueRoute{config}, "", "  ")
	file, e := os.OpenFile(root+"/routes.js", os.O_RDWR|os.O_CREATE, 0777)
	if e != nil {
		println("Wr0", e.Error())
	}
	_, e = file.Write([]byte("export default "))
	if e != nil {
		println("Wr1", e.Error())
	}
	_, e = file.Write(content)
	if e != nil {
		println("Wr2", e.Error())
	}
	file.Close()
	if e != nil {
		println("Wr3", e.Error())
	}

}
func createConfig(root string, path string, config *VueRouteChild) {
	if config.Children == nil {
		config.Children = make([]*VueRouteChild, 0)
	}
	files, e := ioutil.ReadDir(fmt.Sprintf("%s/%s", root, path))
	if e != nil {
		println("E1", e.Error())
	}
	for _, file := range files {
		if file.IsDir() {
			conf := new(VueRouteChild)
			conf.Name = ""
			conf.Path = file.Name()
			curpath := path + "/" + file.Name()
			conf.ComponentPath = fmt.Sprintf("./%s/layout.vue", curpath)
			createConfig(root, curpath, conf)
			config.Children = append(config.Children, conf)
		} else {
			tmp := strings.Split(file.Name(), ".")
			if len(tmp) == 2 && tmp[1] == "vue" && strings.ToLower(tmp[0]) != "layout" {
				conf := new(VueRouteChild)
				conf.Name = ez.CreateRandString(10)
				if strings.ToLower(tmp[0]) != "index" {
					conf.Path = tmp[0]
				}
				curpath := path + "/" + file.Name()
				conf.ComponentPath = fmt.Sprintf("./%s", curpath)
				config.Children = append(config.Children, conf)
			}
		}
	}
}
